package com.qf.dao.Impl;


import com.qf.dao.HealthDao;
import com.qf.entity.Health;
import com.qf.entity.Organ;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HealthDaoImpl implements HealthDao {
    private QueryRunner queryRunner = new QueryRunner();

    //多表查询
    @Override
    public List<Health> selectAllHealth(Page page) {
        String sql = "select health.id id,projectname,status,deadline,money,organId,organname from health\n" +
                "inner join organ o on health.organId = o.id order by health.id asc limit ?,?";
        try {
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStarRow(), page.getPageSize());

            if (query == null) {
                return null;
            }

            List<Health> healthList = new ArrayList<>();
            for (Map<String, Object> map : query) {
                Health health = new Health();
                Organ organ = new Organ();

                BeanUtils.populate(health, map);
                BeanUtils.populate(organ, map);
                health.setOrgan(organ);
                healthList.add(health);
            }
            return healthList;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage() + "查询所有health时出现异常");
        }
    }

    @Override
    public long selectCountHealth() {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select count(*) from health", new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询所有health列出现异常");
        }
    }

    //单表查询单个
    @Override
    public Health selectByHealth(Integer id) {
        try {
            return queryRunner.query(DruidUtil.getConnection(), "select * from health where id =?;", new BeanHandler<>(Health.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "查询单个health出现异常");
        }
    }

    //新增
    @Override
    public int insert(Health health) {
        Object[] param = {health.getProjectname(), health.getStatus(), health.getDeadline(), health.getMoney(), health.getOrganId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(), "insert into health(id,projectname,status,deadline,money,organId) values(null,?,?,?,?,?)", param);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "添加health时出现异常");
        }
    }

    //修改
    @Override
    public int update(Health health) {
        Object[] param = {health.getProjectname(), health.getStatus(), health.getDeadline(), health.getMoney(), health.getOrganId(), health.getId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(), "update health set projectname=?,status=?,deadline=?,money=?,organId=? where id=?", param);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "修改health出现异常");
        }
    }

    @Override
    public Integer delete(Integer id) {
        try {
            return queryRunner.update(DruidUtil.getConnection(), "delete from health where id =?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "删除health出现异常");
        }

    }


}
